package com.lingnan.club.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.club.mapper.PushRecordMapper;
import com.lingnan.club.mapper.UserMapper;
import com.lingnan.club.pojo.PushRecord;
import com.lingnan.club.pojo.User;
import com.lingnan.club.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘宇浩
 * @since 2022-04-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PushRecordMapper pushRecordMapper;

    @Override
    public List<User> orderOnlineTime() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("term_time");
        List<User> list = userMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<User> findOnlineUser() {
        List<User> list = userMapper.findOnlineUser();
        return list;
    }

    @Override
    public List<User> findSignRecord() {
        List<User> userList = userMapper.findFailSignRecord0();
        return userList;
    }

    @Override
    public Boolean pushCard(String id) {
        //判断打卡方式
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("face_id", id);
        //查询出该用户
        User user = userMapper.selectOne(wrapper);
        //存在该用户
        if (!ObjectUtils.isEmpty(user)) {
            //判断是否打卡了
            //当天打过
            if (user.getIsPushCard() == 1) {
                //获取上次打卡时间
                LocalTime beginTime = LocalDateTime.parse(user.getPushDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toLocalTime();
                //获取当前时间
                LocalTime endTime = LocalTime.now();

                //计算在线时长
                int minutes = (int) Duration.between(beginTime, endTime).toMinutes();
                //给用户添加时长
                user.setWeekTime(user.getWeekTime()+minutes);
                user.setMonthTime(user.getMonthTime()+minutes);
                user.setTermTime(user.getTermTime()+minutes);

                int isValid=0;
                if((minutes)>0){
                    isValid=1;//在线时长>1才是有效时长
                }
                //把打卡人id、开始时间、在线时长、是否有效 封装起来
                PushRecord pushRecord = new PushRecord(user.getId(),LocalDateTime.parse(user.getPushDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString() , minutes, isValid);

                //将打卡属性初始化,提交事务
                user.setPushDate(null);
                user.setIsPushCard(0);
                userMapper.update(user, wrapper);

                int insert = pushRecordMapper.insert(pushRecord);
                if (insert > 0) {
                    return true;
                }
            } else {
                //当天未打卡
                //设置开始打卡时间、打卡状态
                user.setPushDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                user.setIsPushCard(1);

                //提交事务
                userMapper.update(user, wrapper);
                return true;
            }
        }
        //没有该用户
        return false;
    }

    @Override
    public boolean resetTerm() {
        User user = new User();
        user.setTermTime(0);
        user.setWeekTime(0);
        user.setMonthTime(0);
        int update = userMapper.update(user, null);
        if (update > 0){
            return true;
        }
        return false;
    }

    @Override
    public User login(User user) {
        User userInfo = getUserInfo(user);
        if (!ObjectUtils.isEmpty(userInfo)){
            return userInfo;
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        if (ObjectUtils.isEmpty(getUserInfo(user))){
            //用户对象存储到数据库
            int insert = userMapper.insert(user);
            if (insert > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public User findMyData(Integer id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    private User getUserInfo(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        // 从数据库查询用户信息
        return userMapper.selectOne(queryWrapper);
    }

}
